Homework 8
Total points: 100
Topics: methods (write several methods; reuse written methods to help you achieve new tasks - see recommended use of add queen to implement delete queen-), 2D arrays (traversal, formatted printing), loop with menu, drawing data, split method, 'distribute' data from one array to other arrays.
Task 1 (15 pts)
Project: HW8_RobotSanta
Objectives: review split to build array from string, write code for problem discussed in class for problem solving, 'distribute' data from one array to other arrays.
Write the program for this Robot Santa problem.
Assume that each present is given by a name consisting of a label (A for Alice or B for Bob) and a toy name. For example "A doll" means it is a doll and it should go to Alice (A), "B train set" means it is a train set and will go to Bob (B).
The presents are given by the user as a list separated by ;.
a) Write a program that reads the list from the user and creates 2 arrays with the presents for Alice and Bob with just the toy name, not the label. See sample run below.
You must create two arrays (AliceArr and BobArr) and store the presents for Alice in AliceArr and the presents for Bob in BobArr.
Hint: Use the split method to quickly turn the string of labelled presents into a 1D array of strings
----------Sample run:
Enter the presents:A doll;B train set;B drawing pad;A car;A Teddy bear;A puzzle;A pen
Alice gets:
doll
car
Teddy bear
puzzle
pen
Bob gets:
train set
drawing pad
EXTEND YOUR THINKING (this part will not be graded): A method can only return one 'thing' in Java (e.g. an int, String, array,...). How would you "put together" AliceArr and BobArr into one 2D array that you could then return from a method?
Task 2a (75 pts)
Project: HW8_PlaceQueens
Objectives: writing a complex program that includes: updating and printing a 2D table (printed table will have labelled rows and columns), error checking. Practice modularization by having to structure the program using methods (reuse written methods to help you achieve new tasks - see recommended use of add queen to implement delete queen-).
This program will maintain and print a chessboard that allows the user to place Queen pieces. The board will show the Queens and the row, column and diagonals the Queens will attack on.
Implement the behaviour shown in the sample run below.
Code structure (a method to do the work for each user option):
- in main - implement a loop that displays the menu information and takes the user option and whatever other data is needed for that option. Next, implement the actions for each choice, using METHODS. DO NOT write a big pile of code in main, but rather call other methods to do the work. Suggested method break-up is given below.
Note that since the same board must be used for as part of the actions for more than one option, the board object should be in main and passed as argument to other methods to work with it.
- a method to create and return a board of a given dimension.
- a method to print the board. (The board will be passed as an argument to this method. Same for the others.)
- a method to reset the board
- a method to add a queen on the board at a given row and column and show with a * in each cell that it can attack. The row and column will be given as arguments, NOT read from the user.
Hint, when you show mark (with *) the cells that are on the diagonal line of attack for a queen, it is easier to go 'from the Q cell out then viceversa':
.
- a method to count queens on the board
- a method to delete a queen from a specific row and column. In addition to the checks for valid board, valid row and valid column, this method should also check that there is a queen at the given position.
You cannot simply delete a queen from the existing board because it is not easy to know which * to remove and which to keep since another queen could be attaching on the same row as the one you are removing (see sample run). Solve this by resetting the board and replacing the queens that should remain.
Take advantage of already implemented methods such as reset board and add queen. (Note how the add queen method can be reused here because the row and column are given as arguments as opposed to read from the user inside the method.)
Other specifications:
See sample runs:
Suggested: start with the menu,argument passing and error checking and you slowly add the functionality. Use method stubs (i.e. a placeholder, or a "dummy" method that matches the signature needed). Ask in class if you want to discuss method stubs further.
Task 2b - Written part (10 points)
Submit the answers for this part in a document called HW8_written.pdf. It must be an electronic document of type pdf, but it can be a scan or photo of a handwritten paper.
Part b (10 points)
Draw the data for the delete queen method that is called FIRST in the delete.txt sample run. (You have the 3 queens that were added and need to delete the queen at row 5 row 3 and column 1.) Show the code on the left side and the drawing on the right side for each main step. A 'main step is: a method call, a complete execution of loop, one iteration of the OUTTER loop in nested loops.
Penalties
All solution files should be placed in a folder called HW8_Lastname. Zip this folder and submit it to Blackboard.
- The submitted homework must use methods as required.
- CODE THAT DOES NOT RUN due to a SYNTAX ERROR 25%-80% penalty per task. Notice that this penalty is up to 80% now. At this point I expect you are able to understand the syntax and fix such problems.
- RUN-TIME ERROR FOR INPUT SPECIFICALLY SHOWED AS AN EXAMPLE: 20% of task grade. Runtime-error for grader’s input: 5-20% of task grade (depending on the reason of why it fails).
- **** Code using elements we had not covered at the time the homework was due, receives no credit.
- Up to 20 points will be lost for non-compliance with the submission requirements: folder name, all files in a folder, zipped folder, the compressed file is a zip, the program files have extension .java, ...
- Each program must have:
- description and your name at the top
- comments
- meaningful variable names
A program missing some or all of these will receive a penalty of up to 10% of the points for that task.